import {
  queryMaintainList, actionMaintainDel, actionMaintainExport, actionMaintainImport, saveAddMaintain,saveEditMaintain,
  queryMaintainInfo, queryMaintainServiceEquipment, actionMaintainChange,
} from '../services/maintain';


export default {
  namespace: 'equipment_maintain',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    serviceEquipment:{},
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo:true,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_maintain_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryMaintainList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },


    // 批量删除操作
    *action_maintain_del({ payload }, { call, put }) {
      const response = yield call(actionMaintainDel, payload);
    },
    // 文件导出
    *action_maintain_export({ payload }, { call, put }) {
      const response = yield call(actionMaintainExport, payload);
    },
    // 文件导入
    *action_maintain_import({ payload }, { call, put }) {
      const response = yield call(actionMaintainImport, payload);
    },
    // 关联设备服务 切换
    *action_maintain_change({ payload }, { call, put }) {
      const response = yield call(actionMaintainChange, payload);
    },


    // 新增
    *add_maintain({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(saveAddMaintain,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 编辑
    *edit_maintain({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(saveEditMaintain,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 获取单个详情
    *fetch_maintain_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryMaintainInfo, payload);
      response && (
        yield put({
          type: 'appendDataDetail',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },


    // 获取单个维保单位关联服务设备
    *fetch_service_equipment({ payload }, { call, put }) {
      const response = yield call(queryMaintainServiceEquipment, payload);
      response && (
        yield put({
          type: 'appendDataServiceEquipment',
          payload:response.data,
        })
      );

    },


  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },
    // appendSelectSource(state, action) {
    //   return {
    //     ...state,
    //     selectSource: {
    //       ...state.selectSource,
    //       ...action.payload,
    //     },
    //   };
    // },

    appendDataServiceEquipment(state, action) {
      return {
        ...state,
        serviceEquipment: action.payload,
      };
    },

    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },


    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
        serviceEquipment:{},
        loading:{}
      };
    },

  },
};
